package cc.lishixi.book.service;

import cc.lishixi.book.dao.mapper.ReaderRowMapper;
import cc.lishixi.book.entity.Reader;
import cc.lishixi.book.service.api.AbstractService;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by LiShixi on 2016/7/24 0024.
 * http://www.lishixi.cc
 */
@Component
public class ReaderService extends AbstractService<Reader> {
    @Override
    public List<Reader> list() {
        return jdbcTemplate.query("select * from reader", new ReaderRowMapper());
    }

    @Override
    public boolean delete(Integer id) {
        return jdbcTemplate.update("delete from reader where readerid=?", id) > 0;
    }

    @Override
    public boolean edit(Reader r) {
        String sql = "UPDATE `book`.`reader` SET `passwd`=?, `name`=?, `gender`=?, `address`=?, `tel`=?, `startdate`=?, `enddate`=?, `type`=? WHERE  `readerid`=1;";
        return jdbcTemplate.update(sql, r.getPasswd(), r.getName(), r.getGender(), r.getAddress(), r.getTel(), r.getStartdate(), r.getEnddate(), r.getType(), r.getReaderid()) > 0;
    }

    @Override
    public boolean add(Reader r) {
        String sql = "INSERT INTO `book`.`reader` ( `passwd`, `name`, `gender`, `address`, `tel`, `startdate`, `enddate`, `type`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?);";
        return jdbcTemplate.update(sql, r.getPasswd(), r.getName(), r.getGender(), r.getAddress(), r.getTel(), r.getStartdate(), r.getEnddate(), r.getType()) > 0;
    }
}
